package org.travel_system.controller;

import jakarta.validation.constraints.Pattern;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.travel_system.pojo.Admin;
import org.travel_system.pojo.User;
import org.travel_system.service.AdminService;
import org.travel_system.service.UserService;
import org.travel_system.utils.JwtUtil;
import org.travel_system.utils.Md5Util;
import org.travel_system.utils.Result;
import org.travel_system.utils.ThreadLocalUtil;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/admin")
public class AdminController {

    private final AdminService adminService;
    private final UserService userService;
    
    @Autowired
    public AdminController(AdminService adminService, UserService userService) {
        this.adminService = adminService;
        this.userService = userService;
    }
    
    //登录管理员
    @PostMapping("/login")
    public Result<?> login(@Pattern(regexp = "^\\S{5,16}$") String adminName,
                           @Pattern(regexp = "^\\S{5,16}$") String password)
    {
        //查询
        Admin loginAdmin = adminService.findAdminByName(adminName);
        //用户是否存在
        if (loginAdmin == null) {
            return Result.error("用户名错误");
        }
        if (Md5Util.getMD5String(password).equals(loginAdmin.getPassword())) {

            //登录成功
            Map<String, Object> claims = new HashMap<>();
            claims.put("id", loginAdmin.getId());
            claims.put("adminName", loginAdmin.getUsername());
            String token = JwtUtil.genToken(claims);

            return Result.success(token);
        }
        return Result.error("密码错误");
    }
    
    //注册管理员
    @PostMapping("/register")
    public Result<?> register(@Pattern(regexp = "^\\S{5,16}$") String adminName,
                              @Pattern(regexp = "^\\S{5,16}$") String password)
    {
        //查询用户
        System.out.println("user:"+adminName);
        return adminService.findAdminByName(adminName) == null &&
                userService.findUserByName(adminName) == null &&
                adminService.register(adminName, password) > 0 ?
                Result.success():Result.error("用户名已被占用");
    }

    //查询管理员信息
    @GetMapping("/info")
    public Result<Admin> getAdminInfo() {
        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("adminName");
        Admin admin = adminService.findAdminByName(username);
        admin.setPassword("");
        admin.setId(adminService.findAdminId(username));
        return Result.success(admin);
    }

    @PutMapping("/info")
    public Result<?> updateAdminInfo(@RequestBody Admin admin) {
        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("adminName");
        admin.setId(adminService.findAdminId(username));
        return adminService.updateAdminInfo(username, admin) > 0 ? Result.success() : Result.error("修改失败");
    }


    //修改密码
    @PatchMapping("password")
    public Result<?> updatePassword(@RequestBody Map<String, String> params) {
        //校验参数
        String oldPwd = params.get("old_pwd");
        String newPwd = params.get("new_pwd");
        String rePwd = params.get("re_pwd");

        if (!StringUtils.hasLength(oldPwd) || !StringUtils.hasLength(newPwd) || !StringUtils.hasLength(rePwd)) {
            return Result.error("缺少必要的参数");
        }

        //原密码是否正确
        Map<String,Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("adminName");
        Admin loginAdmin = adminService.findAdminByName(username);
        if (!loginAdmin.getPassword().equals(Md5Util.getMD5String(oldPwd))){
            return Result.error("原密码错误");
        }

        //原密码和新密码是否一致
        if(loginAdmin.getPassword().equals(Md5Util.getMD5String(newPwd))){
            return Result.error("原密码和新密码不能一样");
        }

        //newPwd和rePwd是否一样
        if (!rePwd.equals(newPwd)){
            return Result.error("两次填写的新密码不一样");
        }

        //密码更新
        return adminService.updatePwd(username, newPwd) > 0 ? Result.success() : Result.error("密码更新失败");
    }

    @GetMapping("/id")
    public Result<Integer> getId(@RequestHeader("Authorization") String token) {
        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("adminName");
        return Result.success(adminService.findAdminId(username));
    }
}
